package org.japo.java.swing.samples;

import java.awt.Color;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import javax.swing.JOptionPane;
import javax.swing.event.ChangeListener;
import javax.swing.text.Document;
import org.japo.java.entities.Rotulo;

public class RotuloBuzonAsistido extends javax.swing.JFrame {
    // Entidades rotulos asociadas a la etiqueta
    private Rotulo[] rotulo = new Rotulo[3];
    private Rotulo rotuloActual;
    
    // Obtener la lista de fuentes del sistema
    String[] listaFuentes = GraphicsEnvironment.getLocalGraphicsEnvironment().
            getAvailableFontFamilyNames();
    
    // Documento del Campo de texto
    Document d;

    public RotuloBuzonAsistido() {
        initComponents();

        // Generar entidades de rótulos
        generarEntidades();

        // Establece el rótulo central como el actual
        rotuloActual = rotulo[1];
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btgLinea = new javax.swing.ButtonGroup();
        pnlMuestra = new javax.swing.JPanel();
        lblArriba = new javax.swing.JLabel();
        lblCentro = new javax.swing.JLabel();
        lblAbajo = new javax.swing.JLabel();
        pnlFuente = new javax.swing.JPanel();
        txfTexto = new javax.swing.JTextField();
        cbbTipografia = new javax.swing.JComboBox(listaFuentes);
        sldTalla = new javax.swing.JSlider();
        lblTalla = new javax.swing.JLabel();
        pnlLinea = new javax.swing.JPanel();
        rbtArriba = new javax.swing.JRadioButton();
        rbtCentro = new javax.swing.JRadioButton();
        rbtAbajo = new javax.swing.JRadioButton();
        pnlDatos = new javax.swing.JPanel();
        btnCargar = new javax.swing.JButton();
        btnGuardar = new javax.swing.JButton();
        pnlColor = new javax.swing.JPanel();
        sldRojo = new javax.swing.JSlider();
        lblRojo = new javax.swing.JLabel();
        sldVerde = new javax.swing.JSlider();
        lblVerde = new javax.swing.JLabel();
        sldAzul = new javax.swing.JSlider();
        lblAzul = new javax.swing.JLabel();
        pnlEstilo = new javax.swing.JPanel();
        cbxNegrita = new javax.swing.JCheckBox();
        cbxCursiva = new javax.swing.JCheckBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Diseño de etiqueta para buzón");
        setResizable(false);

        pnlMuestra.setBackground(new java.awt.Color(255, 255, 255));
        pnlMuestra.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        pnlMuestra.setMaximumSize(new java.awt.Dimension(211, 183));
        pnlMuestra.setMinimumSize(new java.awt.Dimension(211, 183));

        lblArriba.setFont(new java.awt.Font("Tahoma", 0, 36)); // NOI18N
        lblArriba.setForeground(new java.awt.Color(255, 204, 0));
        lblArriba.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblArriba.setText("Rigoberto Picaporte");

        lblCentro.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        lblCentro.setForeground(new java.awt.Color(153, 204, 0));
        lblCentro.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblCentro.setText("Abogado Laboralista");

        lblAbajo.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        lblAbajo.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblAbajo.setText("Entresuelo - Despacho 7B");

        javax.swing.GroupLayout pnlMuestraLayout = new javax.swing.GroupLayout(pnlMuestra);
        pnlMuestra.setLayout(pnlMuestraLayout);
        pnlMuestraLayout.setHorizontalGroup(
            pnlMuestraLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlMuestraLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlMuestraLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblArriba, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(lblCentro, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(lblAbajo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        pnlMuestraLayout.setVerticalGroup(
            pnlMuestraLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlMuestraLayout.createSequentialGroup()
                .addGap(40, 40, 40)
                .addComponent(lblArriba)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblCentro)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblAbajo, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(25, Short.MAX_VALUE))
        );

        pnlFuente.setBorder(javax.swing.BorderFactory.createTitledBorder("Fuente"));

        txfTexto.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txfTexto.setText("Abogado Laboralista");
        d = txfTexto.getDocument();
        d.addDocumentListener(new javax.swing.event.DocumentListener() {
            public void insertUpdate(javax.swing.event.DocumentEvent evt) {
                if (rbtArriba.isSelected()) {
                    lblArriba.setText(txfTexto.getText());
                } else if (rbtCentro.isSelected()) {
                    lblCentro.setText(txfTexto.getText());
                } else if (rbtAbajo.isSelected()) {
                    lblAbajo.setText(txfTexto.getText());
                }
            }
            public void removeUpdate(javax.swing.event.DocumentEvent evt) {
                if (rbtArriba.isSelected()) {
                    lblArriba.setText(txfTexto.getText());
                } else if (rbtCentro.isSelected()) {
                    lblCentro.setText(txfTexto.getText());
                } else if (rbtAbajo.isSelected()) {
                    lblAbajo.setText(txfTexto.getText());
                }
            }
            public void changedUpdate(javax.swing.event.DocumentEvent evt) {
            }
        });

        cbbTipografia.setSelectedItem("Tahoma");
        cbbTipografia.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbbTipografiaActionPerformed(evt);
            }
        });

        sldTalla.setValue(24);
        sldTalla.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                sldTallaStateChanged(evt);
            }
        });

        lblTalla.setBackground(new java.awt.Color(255, 255, 255));
        lblTalla.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblTalla.setText("24");
        lblTalla.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        lblTalla.setOpaque(true);

        javax.swing.GroupLayout pnlFuenteLayout = new javax.swing.GroupLayout(pnlFuente);
        pnlFuente.setLayout(pnlFuenteLayout);
        pnlFuenteLayout.setHorizontalGroup(
            pnlFuenteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlFuenteLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlFuenteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cbbTipografia, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(pnlFuenteLayout.createSequentialGroup()
                        .addComponent(sldTalla, javax.swing.GroupLayout.DEFAULT_SIZE, 223, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblTalla, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(txfTexto))
                .addContainerGap())
        );
        pnlFuenteLayout.setVerticalGroup(
            pnlFuenteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlFuenteLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(txfTexto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(cbbTipografia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 16, Short.MAX_VALUE)
                .addGroup(pnlFuenteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(sldTalla, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblTalla, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        pnlFuenteLayout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {lblTalla, sldTalla});

        pnlLinea.setBorder(javax.swing.BorderFactory.createTitledBorder("Linea"));

        btgLinea.add(rbtArriba);
        rbtArriba.setText("Superior");
        rbtArriba.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbtArribaActionPerformed(evt);
            }
        });

        btgLinea.add(rbtCentro);
        rbtCentro.setSelected(true);
        rbtCentro.setText("Intermedia");
        rbtCentro.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbtCentroActionPerformed(evt);
            }
        });

        btgLinea.add(rbtAbajo);
        rbtAbajo.setText("Inferior");
        rbtAbajo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbtAbajoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnlLineaLayout = new javax.swing.GroupLayout(pnlLinea);
        pnlLinea.setLayout(pnlLineaLayout);
        pnlLineaLayout.setHorizontalGroup(
            pnlLineaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlLineaLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlLineaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(rbtArriba)
                    .addComponent(rbtCentro)
                    .addComponent(rbtAbajo))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        pnlLineaLayout.setVerticalGroup(
            pnlLineaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlLineaLayout.createSequentialGroup()
                .addComponent(rbtArriba, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(rbtCentro, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(rbtAbajo, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pnlDatos.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos"));

        btnCargar.setText("Cargar");
        btnCargar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCargarActionPerformed(evt);
            }
        });

        btnGuardar.setText("Guardar");
        btnGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGuardarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnlDatosLayout = new javax.swing.GroupLayout(pnlDatos);
        pnlDatos.setLayout(pnlDatosLayout);
        pnlDatosLayout.setHorizontalGroup(
            pnlDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlDatosLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnCargar)
                    .addComponent(btnGuardar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pnlDatosLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btnCargar, btnGuardar});

        pnlDatosLayout.setVerticalGroup(
            pnlDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlDatosLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btnCargar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnGuardar))
        );

        pnlColor.setBorder(javax.swing.BorderFactory.createTitledBorder("Color"));

        sldRojo.setMaximum(255);
        sldRojo.setValue(153);
        sldRojo.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                sldRojoStateChanged(evt);
            }
        });

        lblRojo.setBackground(new java.awt.Color(255, 51, 51));
        lblRojo.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lblRojo.setForeground(new java.awt.Color(255, 255, 255));
        lblRojo.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblRojo.setText("153");
        lblRojo.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        lblRojo.setOpaque(true);

        sldVerde.setMaximum(255);
        sldVerde.setValue(204);
        sldVerde.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                sldVerdeStateChanged(evt);
            }
        });

        lblVerde.setBackground(new java.awt.Color(0, 255, 0));
        lblVerde.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lblVerde.setForeground(new java.awt.Color(255, 255, 255));
        lblVerde.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblVerde.setText("204");
        lblVerde.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        lblVerde.setOpaque(true);

        sldAzul.setMaximum(255);
        sldAzul.setValue(0);
        sldAzul.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                sldAzulStateChanged(evt);
            }
        });

        lblAzul.setBackground(new java.awt.Color(0, 0, 255));
        lblAzul.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lblAzul.setForeground(new java.awt.Color(255, 255, 255));
        lblAzul.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblAzul.setText("0");
        lblAzul.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        lblAzul.setOpaque(true);
        lblAzul.setPreferredSize(new java.awt.Dimension(25, 18));

        javax.swing.GroupLayout pnlColorLayout = new javax.swing.GroupLayout(pnlColor);
        pnlColor.setLayout(pnlColorLayout);
        pnlColorLayout.setHorizontalGroup(
            pnlColorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlColorLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlColorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(sldRojo, javax.swing.GroupLayout.DEFAULT_SIZE, 223, Short.MAX_VALUE)
                    .addComponent(sldVerde, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(sldAzul, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlColorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblAzul, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblVerde, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblRojo, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        pnlColorLayout.setVerticalGroup(
            pnlColorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlColorLayout.createSequentialGroup()
                .addGroup(pnlColorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(sldRojo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblRojo))
                .addGap(13, 13, 13)
                .addGroup(pnlColorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(sldVerde, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblVerde))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnlColorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblAzul, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(sldAzul, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(15, Short.MAX_VALUE))
        );

        pnlColorLayout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {lblAzul, lblRojo, lblVerde, sldRojo});

        pnlEstilo.setBorder(javax.swing.BorderFactory.createTitledBorder("Estilo"));

        cbxNegrita.setText("Negrita");
        cbxNegrita.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbxNegritaActionPerformed(evt);
            }
        });

        cbxCursiva.setText("Cursiva");
        cbxCursiva.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbxCursivaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnlEstiloLayout = new javax.swing.GroupLayout(pnlEstilo);
        pnlEstilo.setLayout(pnlEstiloLayout);
        pnlEstiloLayout.setHorizontalGroup(
            pnlEstiloLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlEstiloLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlEstiloLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cbxNegrita)
                    .addComponent(cbxCursiva))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        pnlEstiloLayout.setVerticalGroup(
            pnlEstiloLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlEstiloLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(cbxNegrita)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(cbxCursiva)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(pnlFuente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(pnlColor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(pnlLinea, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(pnlEstilo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(pnlDatos, javax.swing.GroupLayout.DEFAULT_SIZE, 0, Short.MAX_VALUE))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(pnlMuestra, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnlMuestra, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(pnlEstilo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(pnlLinea, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(pnlDatos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(pnlFuente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(pnlColor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-427)/2, (screenSize.height-539)/2, 427, 539);
    }// </editor-fold>//GEN-END:initComponents

    private void rbtArribaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbtArribaActionPerformed
        // Establece el rótulo superior como el actual
        rotuloActual = rotulo[0];

        // Establecer controles
        establecerControles();
    }//GEN-LAST:event_rbtArribaActionPerformed

    private void rbtCentroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbtCentroActionPerformed
        // Establece el rótulo intermedio como el actual
        rotuloActual = rotulo[1];

        // Establecer controles
        establecerControles();
    }//GEN-LAST:event_rbtCentroActionPerformed

    private void btnCargarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCargarActionPerformed
        try {
            cargarDatos();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "Error al cargar los datos");
        }
    }//GEN-LAST:event_btnCargarActionPerformed

    private void btnGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGuardarActionPerformed
        try {
            guardarDatos();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "Error al guardar los datos");
        }
    }//GEN-LAST:event_btnGuardarActionPerformed

    private void rbtAbajoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbtAbajoActionPerformed
        // Establece el rótulo inferior como el actual
        rotuloActual = rotulo[2];

        // Establecer controles
        establecerControles();
    }//GEN-LAST:event_rbtAbajoActionPerformed

    private void sldTallaStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_sldTallaStateChanged
        // Actualiza la fuente
        actualizarFuente();

        // Actualiza la etiqueta de tamaño
        lblTalla.setText("" + sldTalla.getValue());
    }//GEN-LAST:event_sldTallaStateChanged

    private void cbbTipografiaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbbTipografiaActionPerformed
        // Actualiza la fuente
        actualizarFuente();
    }//GEN-LAST:event_cbbTipografiaActionPerformed

    private void cbxNegritaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbxNegritaActionPerformed
        // Actualiza la fuente
        actualizarFuente();
    }//GEN-LAST:event_cbxNegritaActionPerformed

    private void cbxCursivaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbxCursivaActionPerformed
        // Actualiza la fuente
        actualizarFuente();
    }//GEN-LAST:event_cbxCursivaActionPerformed

    private void sldRojoStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_sldRojoStateChanged
        // Actualiza el color
        actualizarColor();

        // Actualiza la etiqueta del componente
        lblRojo.setText("" + sldRojo.getValue());
    }//GEN-LAST:event_sldRojoStateChanged

    private void sldVerdeStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_sldVerdeStateChanged
        // Actualiza el color
        actualizarColor();

        // Actualiza la etiqueta del componente
        lblVerde.setText("" + sldVerde.getValue());
    }//GEN-LAST:event_sldVerdeStateChanged

    private void sldAzulStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_sldAzulStateChanged
        // Actualiza el color
        actualizarColor();

        // Actualiza la etiqueta del componente
        lblAzul.setText("" + sldAzul.getValue());
    }//GEN-LAST:event_sldAzulStateChanged

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
//                if ("Nimbus".equals(info.getName())) {
                if ("Metal".equals(info.getName())) {
//                if ("Windows".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(RotuloBuzonAsistido.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(RotuloBuzonAsistido.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(RotuloBuzonAsistido.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(RotuloBuzonAsistido.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new RotuloBuzonAsistido().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup btgLinea;
    private javax.swing.JButton btnCargar;
    private javax.swing.JButton btnGuardar;
    private javax.swing.JComboBox cbbTipografia;
    private javax.swing.JCheckBox cbxCursiva;
    private javax.swing.JCheckBox cbxNegrita;
    private javax.swing.JLabel lblAbajo;
    private javax.swing.JLabel lblArriba;
    private javax.swing.JLabel lblAzul;
    private javax.swing.JLabel lblCentro;
    private javax.swing.JLabel lblRojo;
    private javax.swing.JLabel lblTalla;
    private javax.swing.JLabel lblVerde;
    private javax.swing.JPanel pnlColor;
    private javax.swing.JPanel pnlDatos;
    private javax.swing.JPanel pnlEstilo;
    private javax.swing.JPanel pnlFuente;
    private javax.swing.JPanel pnlLinea;
    private javax.swing.JPanel pnlMuestra;
    private javax.swing.JRadioButton rbtAbajo;
    private javax.swing.JRadioButton rbtArriba;
    private javax.swing.JRadioButton rbtCentro;
    private javax.swing.JSlider sldAzul;
    private javax.swing.JSlider sldRojo;
    private javax.swing.JSlider sldTalla;
    private javax.swing.JSlider sldVerde;
    private javax.swing.JTextField txfTexto;
    // End of variables declaration//GEN-END:variables

    private void actualizarFuente() {
        // Obtiene la tipografia
        String tipografia = (String) cbbTipografia.getSelectedItem();

        // Obtiene el atributo de negrita
        int negrita = Font.PLAIN;
        if (cbxNegrita.isSelected()) {
            negrita = Font.BOLD;
        }

        // Obtiene el atributo de cursiva
        int cursiva = Font.PLAIN;
        if (cbxCursiva.isSelected()) {
            cursiva = Font.ITALIC;
        }

        // Obtiene el estilo
        int estilo = negrita + cursiva;

        // Obtiene la talla
        int talla = sldTalla.getValue();

        // Genera la fuente
        Font f = new Font(tipografia, estilo, talla);

        // Actualiza la fuente de la entidad actual
        rotuloActual.setFuente(f);

        // Visualiza la nueva fuente
        if (rbtArriba.isSelected()) {
            lblArriba.setFont(f);
        } else if (rbtCentro.isSelected()) {
            lblCentro.setFont(f);
        } else if (rbtAbajo.isSelected()) {
            lblAbajo.setFont(f);
        }
    }

    private void actualizarColor() {
        // Obtiene las componentes de color
        int cR = sldRojo.getValue();
        int cV = sldVerde.getValue();
        int cA = sldAzul.getValue();

        // Instancia un nuevo color
        Color c = new Color(cR, cV, cA);

        // Actualiza el color de la entidad actual
        rotuloActual.setColorFrente(c);

        // Visualiza el nuevo color
        if (rbtArriba.isSelected()) {
            lblArriba.setForeground(c);
        } else if (rbtCentro.isSelected()) {
            lblCentro.setForeground(c);
        } else if (rbtAbajo.isSelected()) {
            lblAbajo.setForeground(c);
        }
    }

    private void generarEntidadArriba() {
        // Texto del rótulo
        String texto = lblArriba.getText();

        // Color del rótulo 
        Color c = lblArriba.getForeground();

        // Fuente del rótulo
        Font f = lblArriba.getFont();

        // Instancia la entidad
        rotulo[0] = new Rotulo(texto, c, Color.WHITE, f);
    }

    private void generarEntidadCentro() {
        // Texto del rótulo
        String texto = lblCentro.getText();

        // Color del rótulo 
        Color c = lblCentro.getForeground();

        // Fuente del rótulo
        Font f = lblCentro.getFont();

        // Instancia la entidad
        rotulo[1] = new Rotulo(texto, c, Color.WHITE, f);
    }

    private void generarEntidadAbajo() {
        // Texto del rótulo
        String texto = lblAbajo.getText();

        // Color del rótulo 
        Color c = lblAbajo.getForeground();

        // Fuente del rótulo
        Font f = lblAbajo.getFont();

        // Instancia la entidad
        rotulo[2] = new Rotulo(texto, c, Color.WHITE, f);
    }

    private void establecerControles() {
        // Almaenes de listeners
        ActionListener[] al;
        ChangeListener[] cl;

        // Texto del rótulo
        txfTexto.setText(rotuloActual.getTexto());

        // Tipografia
        al = cbbTipografia.getActionListeners();
        for (int i = 0; i < al.length; i++) {
            cbbTipografia.removeActionListener(al[i]);
        }
        cbbTipografia.setSelectedItem(rotuloActual.getFuente().getFamily());
        for (int i = 0; i < al.length; i++) {
            cbbTipografia.addActionListener(al[i]);
        }

        // Tamaño del texto
        cl = sldTalla.getChangeListeners();
        for (int i = 0; i < cl.length; i++) {
            sldTalla.removeChangeListener(cl[i]);
        }
        sldTalla.setValue(rotuloActual.getFuente().getSize());
        for (int i = 0; i < cl.length; i++) {
            sldTalla.addChangeListener(cl[i]);
        }
        lblTalla.setText("" + rotuloActual.getFuente().getSize());

        // Estilo
        int estilo = rotuloActual.getFuente().getStyle();
        if (estilo == Font.PLAIN) {
            cbxNegrita.setSelected(false);
            cbxCursiva.setSelected(false);
        } else if (estilo == Font.BOLD) {
            cbxNegrita.setSelected(true);
            cbxCursiva.setSelected(false);
        } else if (estilo == Font.ITALIC) {
            cbxNegrita.setSelected(false);
            cbxCursiva.setSelected(true);
        } else {
            cbxNegrita.setSelected(true);
            cbxCursiva.setSelected(true);
        }

        // Color ROJO
        cl = sldRojo.getChangeListeners();
        for (int i = 0; i < cl.length; i++) {
            sldRojo.removeChangeListener(cl[i]);
        }
        sldRojo.setValue(rotuloActual.getColorFrente().getRed());
        for (int i = 0; i < cl.length; i++) {
            sldRojo.addChangeListener(cl[i]);
        }
        lblRojo.setText("" + rotuloActual.getColorFrente().getRed());

        // Color VERDE
        cl = sldVerde.getChangeListeners();
        for (int i = 0; i < cl.length; i++) {
            sldVerde.removeChangeListener(cl[i]);
        }
        sldVerde.setValue(rotuloActual.getColorFrente().getGreen());
        for (int i = 0; i < cl.length; i++) {
            sldVerde.addChangeListener(cl[i]);
        }
        lblVerde.setText("" + rotuloActual.getColorFrente().getGreen());

        // Color AZUL
        cl = sldAzul.getChangeListeners();
        for (int i = 0; i < cl.length; i++) {
            sldAzul.removeChangeListener(cl[i]);
        }
        sldAzul.setValue(rotuloActual.getColorFrente().getBlue());
        for (int i = 0; i < cl.length; i++) {
            sldAzul.addChangeListener(cl[i]);
        }
        lblAzul.setText("" + rotuloActual.getColorFrente().getBlue());
    }

    private void cargarDatos() throws Exception {
        BufferedReader entrada = null;
        StringTokenizer st;
        String linea;

        try {
            // Conecta el fichero de datos
            entrada = new BufferedReader(new FileReader("rotulo.txt"));

            // Recorre el fichero de datos
            for (int i = 0; i < 3; i++) {
                // Lee los datos del rótulo actual
                linea = entrada.readLine();

                // Procesa la linea actual
                st = new StringTokenizer(linea, ",");

                // Texto del rótulo
                rotulo[i].setTexto(st.nextToken());

                // Color de frente del rótulo
                int cR = Integer.parseInt(st.nextToken());
                int cV = Integer.parseInt(st.nextToken());
                int cA = Integer.parseInt(st.nextToken());
                rotulo[i].setColorFrente(new Color(cR, cV, cA));

                // Color de fondo del rótulo
                rotulo[i].setColorFondo(Color.WHITE);

                // Fuente del rótulo
                String tipografia = st.nextToken();
                int estilo = Integer.parseInt(st.nextToken());
                int talla = Integer.parseInt(st.nextToken());
                rotulo[i].setFuente(new Font(tipografia, estilo, talla));
            }
            
            // Actualiza la visualización del rótulo de arriba
            lblArriba.setText(rotulo[0].getTexto());
            lblArriba.setForeground(rotulo[0].getColorFrente());
            lblArriba.setFont(rotulo[0].getFuente());
            
            // Actualiza la visualización del rótulo del centro
            lblCentro.setText(rotulo[1].getTexto());
            lblCentro.setForeground(rotulo[1].getColorFrente());
            lblCentro.setFont(rotulo[1].getFuente());
            
            // Actualiza la visualización del rótulo de abajo
            lblAbajo.setText(rotulo[2].getTexto());
            lblAbajo.setForeground(rotulo[2].getColorFrente());
            lblAbajo.setFont(rotulo[2].getFuente());
            
            // Establece el rótulo superior como el actual
            rbtArriba.doClick();
            
            // Establecer controles
//            establecerControles();
        } finally {
            if (entrada != null) {
                entrada.close();
            }
        }
    }

    private void generarEntidades() {
        // Configura el rótulo superior
        generarEntidadArriba();

        // Configura el rótulo intermedio
        generarEntidadCentro();

        // Configura el rótulo inferior
        generarEntidadAbajo();
    }

    private void guardarDatos() throws Exception {
        PrintWriter salida = null;
        String linea;

        try {
            // Conecta el fichero de datos
            salida = new PrintWriter(new FileWriter("rotulo.txt"));

            // Recorre las entidades de datos
            for (int i = 0; i < 3; i++) {
                // Texto del rótulo
                linea = rotulo[i].getTexto() + ",";

                // Color de frente del rótulo
                int cR = rotulo[i].getColorFrente().getRed();
                int cV = rotulo[i].getColorFrente().getGreen();
                int cA = rotulo[i].getColorFrente().getBlue();
                linea += cR + "," + cV + "," + cA + ",";

                // Fuente del rótulo
                String tipografia = rotulo[i].getFuente().getFamily();
                int estilo = rotulo[i].getFuente().getStyle();
                int talla = rotulo[i].getFuente().getSize();
                linea += tipografia + "," + estilo + "," + talla;
                
                // Escribe la linea de texto
                salida.println(linea);
            }
        } finally {
            if (salida != null) {
                salida.close();
            }
        }
    }
}
